package com.platform.system.mqtt.event.listener;

import com.platform.common.constant.Constants;
import com.platform.system.domain.dto.device.DeviceInfoDto;
import com.platform.system.domain.vo.device.DeviceInfoVo;
import com.platform.system.mqtt.event.DeviceListenerEvent;
import com.platform.system.service.ISysDeviceInfoService;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Huangkai
 */
@Component
public class DeviceEventListener {

    @Resource
    ISysDeviceInfoService sysDeviceInfoService;

    @EventListener
    public void handleOrderEvent(DeviceListenerEvent event) {
        DeviceInfoDto deviceInfoDto = event.getDeviceInfoDto();
        if(event.getType().equals(Constants.NUM_ONE)){
            //在线
            DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
            deviceInfoVo.setUid(deviceInfoDto.getUid());
            deviceInfoVo.setStatus(Constants.NUM_ONE);
            sysDeviceInfoService.updateDeviceInfoStatus(deviceInfoVo);
        }else if(event.getType().equals(Constants.NUM_ZERO)){
            //离线
            DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
            deviceInfoVo.setUid(deviceInfoDto.getUid());
            deviceInfoVo.setStatus(Constants.NUM_ZERO);
            sysDeviceInfoService.updateDeviceInfoStatus(deviceInfoVo);
        }
    }
}
